package com.icangya.admin;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.icangya.Utils.JSONUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.icangya.Utils.CheckSumBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 发送验证码
 * @author liuxuanlin
 *
 */
@CrossOrigin
@RestController
@RequestMapping(path = "/foot/sendCode")
public class SendCodeController {

    //发送验证码的请求路径URL
    private static final String SERVER_URL="https://api.netease.im/sms/sendcode.action";
    //网易云信分配的账号，请替换你在管理后台应用下申请的Appkey
    private static final String APP_KEY="72e47dd02600a61ac040900b12908613";
    //网易云信分配的密钥，请替换你在管理后台应用下申请的appSecret
    private static final String APP_SECRET="f35f14e425dd";
    //随机数
    private static final String NONCE="123456";
    //短信模板ID
    private static final String TEMPLATEID="3052797";
    //手机号
    private static final String MOBILE="18874174923";
    //验证码长度，范围4～10，默认为4
    private static final String CODELEN="6";


    private Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     *发送短信验证码
     * @param phone   手机号码
     */
    @RequestMapping(value = "sendCode")
    public JSONObject sendCode(@RequestParam String phone) throws Exception{
//        String mobileNumber = "18874174923";//接收验证码的手机号码


        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost post = new HttpPost(SERVER_URL);

        String curTime=String.valueOf((new Date().getTime()/1000L));
        String checkSum=CheckSumBuilder.getCheckSum(APP_SECRET,NONCE,curTime);

        //设置请求的header
        post.addHeader("AppKey",APP_KEY);
        post.addHeader("Nonce",NONCE);
        post.addHeader("CurTime",curTime);
        post.addHeader("CheckSum",checkSum);
        post.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");

        //设置请求参数
        List<NameValuePair> nameValuePairs =new ArrayList<>();
        nameValuePairs.add(new BasicNameValuePair("mobile",phone));

        post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));

        //执行请求
        HttpResponse response=httpclient.execute(post);
        String responseEntity= EntityUtils.toString(response.getEntity(),"utf-8");

        //判断是否发送成功，发送成功返回true
        String code= JSON.parseObject(responseEntity).getString("code");
        if (code.equals("200")){
            return JSONUtils.getRoot(0, "success",null);
        }
        return JSONUtils.getRoot(1, "fail",null);

    }



    /**
     *校验验证码
     * @param phone   手机号码
     */
    @RequestMapping(value = "checkSendCode")
    public JSONObject checkSendCode(@RequestParam String phone,@RequestParam String sum) throws Exception{

        final String SERVER_URL="https://api.netease.im/sms/verifycode.action";//校验验证码的请求路径URL

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost post = new HttpPost(SERVER_URL);

        String curTime=String.valueOf((new Date().getTime()/1000L));
        String checkSum=CheckSumBuilder.getCheckSum(APP_SECRET,NONCE,curTime);

        //设置请求的header
        post.addHeader("AppKey",APP_KEY);
        post.addHeader("Nonce",NONCE);
        post.addHeader("CurTime",curTime);
        post.addHeader("CheckSum",checkSum);
        post.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");

        //设置请求参数
        List<NameValuePair> nameValuePairs =new ArrayList<>();
        nameValuePairs.add(new BasicNameValuePair("mobile",phone));
        nameValuePairs.add(new BasicNameValuePair("code",sum));

        post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));

        //执行请求
        HttpResponse response=httpclient.execute(post);
        String responseEntity= EntityUtils.toString(response.getEntity(),"utf-8");

        //判断是否发送成功，发送成功返回true
        String code= JSON.parseObject(responseEntity).getString("code");
        if (code.equals("200")){
            return JSONUtils.getRoot(0, "success",null);
        }
        return JSONUtils.getRoot(1, "fail",null);
    }






        /**
         *发送短信验证码
         * @param phone   手机号码
         */
        public   String sendMsg(String phone)throws Exception{
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpPost post = new HttpPost(SERVER_URL);

            String curTime=String.valueOf((new Date().getTime()/1000L));
            String checkSum=CheckSumBuilder.getCheckSum(APP_SECRET,NONCE,curTime);

            //设置请求的header
            post.addHeader("AppKey",APP_KEY);
            post.addHeader("Nonce",NONCE);
            post.addHeader("CurTime",curTime);
            post.addHeader("CheckSum",checkSum);
            post.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");

            //设置请求参数
            List<NameValuePair> nameValuePairs =new ArrayList<>();
            nameValuePairs.add(new BasicNameValuePair("mobile",phone));

            post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));

            //执行请求
            HttpResponse response=httpclient.execute(post);
            String responseEntity= EntityUtils.toString(response.getEntity(),"utf-8");

            //判断是否发送成功，发送成功返回true
            String code= JSON.parseObject(responseEntity).getString("code");
            if (code.equals("200")){
                return "success";
            }
            return "error";

    }

        /**
         * 校验验证码
        * */
        public   String checkMsg(String phone,String sum)throws  Exception{
          final String SERVER_URL="https://api.netease.im/sms/verifycode.action";//校验验证码的请求路径URL

            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpPost post = new HttpPost(SERVER_URL);

            String curTime=String.valueOf((new Date().getTime()/1000L));
            String checkSum=CheckSumBuilder.getCheckSum(APP_SECRET,NONCE,curTime);

            //设置请求的header
            post.addHeader("AppKey",APP_KEY);
            post.addHeader("Nonce",NONCE);
            post.addHeader("CurTime",curTime);
            post.addHeader("CheckSum",checkSum);
            post.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");

            //设置请求参数
            List<NameValuePair> nameValuePairs =new ArrayList<>();
            nameValuePairs.add(new BasicNameValuePair("mobile",phone));
            nameValuePairs.add(new BasicNameValuePair("code",sum));

            post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));

            //执行请求
            HttpResponse response=httpclient.execute(post);
            String responseEntity= EntityUtils.toString(response.getEntity(),"utf-8");

            //判断是否发送成功，发送成功返回true
            String code= JSON.parseObject(responseEntity).getString("code");
            if (code.equals("200")){
                return "success";
            }
            return "error";
        }





}
